<?php
	session_start();
	if(!isset($_SESSION["jara_admin"])) {
		header("Location: ../login.php");
		exit;
	}
	if($_SESSION["jara_permissions"][2] == 0) {	
		header("Location: error.php");
		exit;
	}
	require_once("../include/templating_fns.php");
	require_once("../include/db_fns.php");
	jara_page_start("Choose Widgets - Administrator CP");
	if(isset($_REQUEST["edit_action"])) {
		try {
			$widgets = array(0, 0, 0, 0);
			if(isset($_POST["widget1"])) {
				$widgets[0] = 1;
			}
			if(isset($_POST["widget2"])) {
				$widgets[1] = 1;
			}
			if(isset($_POST["widget3"])) {
				$widgets[2] = 1;
			}
			if(isset($_POST["widget4"])) {
				$widgets[3] = 1;
			}
			$query = "update jara_settings set value = '{$widgets[0]}' where name = 'widget_search'";
			$result = jara_db_query($query);
			if($result == 0) {
				$query = "insert into jara_settings values(NULL, 'widget_search', '{$widgets[0]}', 'Search Widget On')";
				jara_db_query($query);
			}
			$query = "update jara_settings set value = '{$widgets[1]}' where name = 'widget_user'";
			$result = jara_db_query($query);
			if($result == 0) {
				$query = "insert into jara_settings values(NULL, 'widget_user', '{$widgets[1]}', 'User Widget On')";
				jara_db_query($query);
			}
			$query = "update jara_settings set value = '{$widgets[2]}' where name = 'widget_recent_posts'";
			$result = jara_db_query($query);
			if($result == 0) {
				$query = "insert into jara_settings values(NULL, 'widget_recent_posts', '{$widgets[2]}', 'Recent Posts Widget On')";
				jara_db_query($query);
			}
			$query = "update jara_settings set value = '{$widgets[3]}' where name = 'widget_categories'";
			$result = jara_db_query($query);
			if($result == 0) {
				$query = "insert into jara_settings values(NULL, 'widget_categories', '{$widgets[3]}', 'Categories Widget On')";
				jara_db_query($query);
			}
			echo "<p>Changed widgets successfully. Please navigate to another page to see the changes.</p>";
			jara_page_end();
			exit;
		}
		catch(JaraDatabaseException $ex) {
?>
	<p>
		A database error occured during the process of creating this page.
	</p>
	<p>
		Error type: <?php echo (($ex->getSqlStage() == 1) ? "MySQL connection error" : "MySQL query error"); ?><br />
		Error message: &quot;<?php echo $ex->getSqlMsg(); ?>&quot; [code <?php echo $ex->getSqlCode(); ?>]
	</p>
<?php
			jara_page_end();
			exit;
		}
		catch(JaraGeneralException $ex) {
?>
	<p>
		A general error occured during the process of creating this page.
	</p>
	<p>
		Error message: &quot;<?php echo $ex->getMessage(); ?>&quot;
	</p>
<?php
			jara_page_end();
			exit;
		}
		exit;
	}
?>
<form action="choose_widgets.php" method="post">
<p>
<input type="hidden" name="edit_action" id="edit_action" value="change" />
<h3>Choose Widgets</h3>
<p>
<input type="checkbox" name="widget1" id="widget1" /> Search Widget<br />
<input type="checkbox" name="widget2" id="widget2" /> User Widget<br />
<input type="checkbox" name="widget3" id="widget3" /> Recent Posts Widget<br />
<input type="checkbox" name="widget4" id="widget4" /> Categories Widget
</p>
<p>
<input type="submit" value="Save" />
</p>
</form>
<?php
	jara_page_end();
?>